package com.itheima.day4.analysis;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.stream.Stream;

import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;

public class AnalysisTest05 {
    /*
        数据格式
        0       1       2       3       4       5       6     7      8        9     10    11
        序号    下单时间  订单编号  商品编号 类别编号  类别码   品牌   价格   用户编号   年龄   性别   地区
     */
    static final int INDEX = 0;
    static final int TIME = 1;
    static final int ORDER_ID = 2;
    static final int PRODUCT_ID = 3;
    static final int CATEGORY_ID = 4;
    static final int CATEGORY_CODE = 5;
    static final int BRAND = 6;
    static final int PRICE = 7;
    static final int USER_ID = 8;
    static final int USER_AGE = 9;
    static final int USER_SEX = 10;
    static final int USER_REGION = 11;
    static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");

    public static void main(String[] args) {
        case51();
    }

    // 5.1) 每个地区下单最多的用户
    private static void case51() {
        try (Stream<String> lines = Files.lines(Path.of("./data.txt"))) {
            Map<String, Map<String, Long>> collect = lines.skip(1)
                    .map(line -> line.split(","))
                    .collect(groupingBy(array -> array[USER_REGION], groupingBy(array -> array[USER_ID], counting())));

            collect.entrySet().stream()
                    .map(e -> Map.entry(
                            e.getKey(),
                            e.getValue().entrySet().stream()
                                    .max(Map.Entry.comparingByValue())
                    ))
                    .forEach(System.out::println);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    static class MyQueue<E> extends PriorityQueue<E> {
        private final int max;

        public MyQueue(Comparator<? super E> comparator, int max) {
            super(comparator);
            this.max = max;
        }

        @Override
        public boolean offer(E e) {
            boolean r = super.offer(e);
            if (this.size() > max) {
                this.poll();
            }
            return r;
        }
    }

    // 5.2) 每个地区下单最多的前三用户
    private static void case52() {
        try (Stream<String> lines = Files.lines(Path.of("./data.txt"))) {
            Map<String, Map<String, Long>> collect = lines.skip(1)
                    .map(line -> line.split(","))
                    .collect(groupingBy(array -> array[USER_REGION], groupingBy(array -> array[USER_ID], counting())));

            collect.entrySet().stream()
                    .map(e -> Map.entry(
                            e.getKey(),
//                            e.getValue().entrySet().stream()
//                                    .sorted(Map.Entry.<String, Long>comparingByValue().reversed())
//                                    .limit(3)
//                                    .collect(toList())
                            e.getValue().entrySet().stream()
                                    .collect(
                                            () -> new MyQueue<Map.Entry<String, Long>>(Map.Entry.comparingByValue(), 3),
                                            MyQueue::offer,
                                            MyQueue::addAll
                                    )
                    ))
                    .forEach(System.out::println);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
